<?php
/**
 * Created by IntelliJ IDEA.
 * User: luwei
 * Date: 2017/7/25
 * Time: 15:44
 */

namespace app\modules\api\models;

use app\hejiang\ApiCode;
use app\hejiang\ApiResponse;
use app\hejiang\BaseApiResponse;
use app\models\PurchaseGoods;
use app\models\Purchase;
use app\models\Model;
use app\models\PurchaseMoneyLog;
use app\models\User;
use app\models\Order;

class PurchaseForm extends ApiModel
{
    public $store_id;
    public $model;
	public $user_id;
    public $goods_list;

    public $review_result;


    public function rules()
    {
        return [
            [['store_id','user_id'], 'integer'],
            [['user_id'], 'integer']
        ];
    }

    public function attributeLabels() 
    { 
        return [ 
            'store_id' => 'Store ID',
            'user_id' => '用户'
           
        ]; 
    }

    public function search()
    {
		
        if (!$this->validate())
            return $this->errorResponse;
        $purchaseInfo = Purchase::find()
		->where([
            'user_id' => $this->user_id,
            'store_id' => $this->store_id,
        ])->one();
		$user = User::findOne(['id' => $purchaseInfo['user_id']]);
		if($user){
			$userInfo['nickname'] = $user['nickname'];
			$userInfo['avatar_url'] = $user['avatar_url'];
		}
		$money1 = Order::find()->select('sum(purchaseMoney) as money')->where([
            'purchase_id' => $purchaseInfo['id'],
            'is_pay' => 1,
        ])->asArray()->one();
		$money = PurchaseMoneyLog::find()->select('sum(money) as money')->where([
            'user_id' => $this->user_id,
            'store_id' => $this->store_id,
        ])->asArray()->one();
		if( $money && $money1){
			
			$userInfo['money'] = $money['money']?$money['money']:0;
			$userInfo['moneyall'] = $money1['money']?$money1['money']:0;
		}else{
			$userInfo['money'] = 0;
		}
        $data = [
            'purchaseInfo' => $purchaseInfo,
			'userInfo'=>$userInfo
			
        ];
	
        return new ApiResponse(0, 'success', $data);
    }
}
